package com.msh.artascope.starter.web.config;


import com.msh.artascope.interfaces.*;
import com.msh.artascope.web.common.api.LoginInfoApi;
import com.msh.artascope.web.common.bean.ConfigLoginAccount;
import com.msh.artascope.web.common.bean.ConfigLoginAuth;
import com.msh.artascope.web.common.bean.GatewayAccountGat;
import com.msh.artascope.web.common.config.ConfigLoginConfiguration;
import com.msh.artascope.web.common.interceptor.ArtascopeAccountInterceptor;
import com.msh.artascope.web.common.interceptor.LocalDevelopmentInterceptor;
import com.msh.frame.client.interfaces.IClassLoadLog;
import com.msh.frame.web.util.HttpServletResponseUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.servlet.http.HttpServletResponse;

@Configuration
public class ArtascopeStarterWebAutoConfiguration implements IClassLoadLog {
  private boolean gatewayAccountGetLoad = false;
  private boolean loginInfoApiLoad = false;
  private boolean artascopeAccountInterceptorLoad = false;
  private boolean localDevelopmentInterceptorLoad = false;
  private boolean configLoginConfigurationLoad = false;
  private boolean configLoginAccountLoad = false;
  private boolean configLoginAuthLoad = false;
  private boolean loginLoad = false;


  // --------------- 本地登录服务 start ------------------------------------------

  @Bean
  @ConditionalOnProperty(name = "msh.login.mode", havingValue = "local")
  public ConfigLoginConfiguration configLoginConfiguration(){
    configLoginConfigurationLoad = true;
    return new ConfigLoginConfiguration();
  }

  /**
   * 登录账号信息(本地不登录直接返回，通过配置可返回具体账号)
   * 通过配置直接返回登录账号
   * @param configuration
   * @return
   */
  @Bean
  @ConditionalOnBean(ConfigLoginConfiguration.class)
  public ILoginAccount configLoginAccount(ConfigLoginConfiguration configuration){
    configLoginAccountLoad = true;
    return new ConfigLoginAccount(configuration);
  }

  @Bean
  @ConditionalOnBean(ConfigLoginConfiguration.class)
  public IAccountAuth configLoginAuth(ConfigLoginConfiguration configuration){
    configLoginAuthLoad = true;
    return new ConfigLoginAuth(configuration);
  }


  @Bean
  @ConditionalOnBean({ILoginAccount.class, IAccountAuth.class})
  public ILogin login(ILoginAccount loginAccount, IAccountAuth accountAuth){
    loginLoad = true;
    return new ILogin() {
      @Override
      public IVerifyInfo verifyCodeImg() {
        return new IVerifyInfo() {
          @Override
          public String getUniqueKey() {
            return "111111";
          }

          @Override
          public Long getExpireTime() {
            return 300L;
          }

          @Override
          public String getInfo() {
            return "";
          }
        };
      }

      @Override
      public ILoginAccount loginIn(ILoginInfo loginInfo) {
        HttpServletResponse currentResponse = HttpServletResponseUtil.getCurrentResponse();
        currentResponse.addHeader("artascope-token-dev", "local");
        return loginAccount;
      }

      @Override
      public void loginOut() {
      }

      @Override
      public void modifyPassword(IModifyPassword modifyPassword) {

      }

      @Override
      public ILoginAccount getLoginAccount() {
        return loginAccount;
      }

      @Override
      public IAccountAuth getAccountAuth() {
        return accountAuth;
      }

    };
  }


  /**
   * 本地服务登录接口
   * 只要实现 ILogin 接口就可加载前端登录接口
   * /api/login
   * @param login
   * @return
   */
  @Bean
  @ConditionalOnBean(ILogin.class)
  public LoginInfoApi loginInfoApi(ILogin login){
    loginInfoApiLoad = true;
    return new LoginInfoApi(login);
  }


  // --------------- 本地登录服务 end ------------------------------------------


  /**
   * 通过网关获取账号
   * @return
   */
  @Bean
  @ConditionalOnMissingBean(IAccountGet.class)
  public IAccountGet gatewayAccountGet(){
    gatewayAccountGetLoad = true;
    return new GatewayAccountGat();
  }

  @Bean
  @ConditionalOnBean(GatewayAccountGat.class)
  @ConditionalOnProperty(name = "msh.development.local.enable", havingValue = "true")
  public LocalDevelopmentInterceptor localDevelopmentInterceptor(){
    localDevelopmentInterceptorLoad = true;
    return new LocalDevelopmentInterceptor();
  }

  @Bean
  @ConditionalOnBean(GatewayAccountGat.class)
  public ArtascopeAccountInterceptor artascopeAccountInterceptor(IAccountGet accountGet){
    artascopeAccountInterceptorLoad = true;
    return new ArtascopeAccountInterceptor(accountGet);
  }

  @Override
  public String logInfo() {
    StringBuilder sb = new StringBuilder()
        .append("----执行类 ")
        .append(ArtascopeStarterWebAutoConfiguration.class)
        .append("\r\n");
    if(gatewayAccountGetLoad){
      sb.append("--------加载类 ")
          .append(GatewayAccountGat.class)
          .append("\r\n");
    }
    if(loginInfoApiLoad){
      sb.append("--------加载类 ")
          .append(LoginInfoApi.class)
          .append("\r\n");
    }
    if(artascopeAccountInterceptorLoad){
      sb.append("--------加载类 ")
          .append(ArtascopeAccountInterceptor.class)
          .append("\r\n");
    }
    if(localDevelopmentInterceptorLoad){
      sb.append("--------加载类 ")
          .append(LocalDevelopmentInterceptor.class)
          .append("\r\n");
    }
    if(configLoginConfigurationLoad){
      sb.append("--------加载类 ")
              .append(ConfigLoginConfiguration.class)
              .append("\r\n");
    }
    if(configLoginAccountLoad){
      sb.append("--------加载类 ")
              .append(ConfigLoginAccount.class)
              .append("\r\n");
    }
    if(configLoginAuthLoad){
      sb.append("--------加载类 ")
              .append(ConfigLoginAuth.class)
              .append("\r\n");
    }
    if(loginLoad){
      sb.append("--------加载类 ")
              .append(ILogin.class)
              .append("\r\n");
    }
    return sb.toString();
  }
}
